Source Code For Projectile Function

by Tom Nally Steelweaver52@aol.com

Return to article, Persuading Functions to Return Multiple Values



'''''''''''''''''''''''''''''''''''''''''
' proj_function.bas                     '
'                                       '
' A function which allows a programmer  '
' to calculate the coordinates of a     '
' projecile shot into the air.          '
'                                       '
' By Tomas J. Nally                     '
' Steelweaver52@aol.com                 '
'                                       '
' Released as open source               '
'''''''''''''''''''''''''''''''''''''''''
' Made with Liberty BASIC               '
' by Carl Gundel                        '
' http://www.libertybasic.com           '
'                                       '
' Made with Liberty BASIC Workshop      '
' by Alyce Watson                       '
' http://alycesrestaurant.com           '
'''''''''''''''''''''''''''''''''''''''''

'Provide the x-, y- and z-coordinates of the 
'projectile launch location...

launchX = 50
launchY = 5
launchZ = 85

'Provide the swivel angle in degrees.  Imagine
'the barrel of the cannon projected onto the 
'horizontal plane (the X-Z plane).  The angle that
'the barrel projection makes with the X-axis is the
'swivel angle.

swivelDegrees = 31

'Provide the tilt angle of the cannon in degrees.
'The tilt angle is the angle at which the cannon
'barrel is tilted up from the horizontal plane.

tiltDegrees = 63

'Provide the muzzle velocity.  This is the velocity
'of the projectile as it leaves the muzzle of the
'cannon.

muzzVel = 150  'units should be feet/second

'Provide the time, t, at which you want the x-,y-,
'and z-position of the projectile after it has been
'fired.  Usually, a programmer will want the position
'of the projectile over many shortly-spaced time
'increments.  For instance, a programmer might want
'to know where the projectile is every fifth of 
'a second over the first six seconds of the 
'projectile's flight.  So, let's vary "t" within
'a loop and print out the coordinates of the 
'projectile at each time, t.

print "Projectile Coordinates"
print "----------------------"
print "  "

for i = 0 to 40
    
    t = i/5   'that is, find the coordinates every fifth of a second
    
    'Call the function which returns the position 
    'of the projectile as a string...
    
    positionXYZ$ = ProjectilePosition$(launchX, launchY, launchZ, swivelDegrees, tiltDegrees, muzzVel, t)
    
    'Parse the positionXYZ$ variable into the three numerical position
    'components of the projectile...

    positionX = val(word$(positionXYZ$, 1))
    positionY = val(word$(positionXYZ$, 2))
    positionZ = val(word$(positionXYZ$, 3))
    
    'Print the current time, t, as well as the
    'coordinates of the projectile at time, t
    
    print "t="; using("##.##", t);
    print "  x="; using("####.##", positionX);
    print "  y="; using("####.##", positionY);
    print "  z="; using("####.##", positionZ)
    
next i

'Wait for [RETURN] from the user before the window is closed...

print "  "
input "Press [RETURN] to end program "; AAA$

end


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  This is the actual projectile function...
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ProjectilePosition$(launchX, launchY, launchZ, swivelDegrees, tiltDegrees, muzzVel, t)

    'Define the constant, pi
    pi = 3.14159
    
    'Define the gravitational constant
    g = (-32.2)   'units are in feet/second^2
    
    'Convert swivelDegrees to radians
    swivelRadians = (swivelDegrees/360)*2*pi
    
    'Convert tiltDegrees to radians
    tiltRadians = (tiltDegrees/360)*(2*pi)
    
    'If the cannonbarrel would have a unit length of 1,
    'find the length of the barrel's projection onto 
    'the X-Z plane.
    
    projectedLength = 1*cos(tiltRadians)
    
    'find the component lengths of the "unit cannon"
    'in the x-, y- and z-directions...
    
    compx = projectedLength * cos(swivelRadians)
    compz = projectedLength * sin(swivelRadians)
    compy = 1 * sin(tiltRadians)

    'Since the length of the "unit cannon" by definition is 1,
    'the direction cosines are exactly equal to the 
    'component lengths...
    
    directioncosinex = compx
    directioncosiney = compy
    directioncosinez = compz

    'Find the x-, y- and z- components of the muzzle velocity
    'by mulitplying muzzVel by the direction cosines...
    
    compvelx = (directioncosinex * muzzVel)
    compvely = (directioncosiney * muzzVel)
    compvelz = (directioncosinez * muzzVel)

    'All the preliminary work is done.  Now, there is enough
    'information in place to compute the position of the
    'projectile at time, t...

    projectileX = launchX + (compvelx * t)
    projectileY = launchY + (compvely * t) + (1/2)*g*t^2
    projectileZ = launchZ + (compvelz * t)

    'Convert the three numerical components of the projectile's
    'position into one large string.  Make sure that "spacer strings"
    'are used between the components so that the numbers can be parsed...
    
    ProjectilePosition$ = str$(projectileX) + "  " + str$(projectileY) + "  " + str$(projectileZ)

end function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                                                                           



Return to article, Persuading Functions to Return Multiple Values